package com.qfmy.app.task;

import com.qfmy.app.mapper.novel.ReadTimeMapper;
import com.qfmy.model.entity.novel.ReadTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 清风明月
 * @Package com.qfmy.app.task
 * @date 2025/4/16
 * @description 小说的定时任务
 */
@Component
@SuppressWarnings("all")
public class BookTask {
    /**
     * 注入mapper
     */
    @Autowired
    private ReadTimeMapper readTimeMapper;

    /**
     * 更新用户的阅读时间
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void updateSearchCount() {
        //获取所有的阅读时间
        List<ReadTime> readTimes = readTimeMapper.selectAll();
        //循环遍历
        for (ReadTime readTime : readTimes) {
            //获取一天的阅读时间
            Integer dayTime = Integer.valueOf(readTime.getDayTime());
            readTime.setMonthTime(String.valueOf(readTime.getMonthTime() + dayTime));
            readTime.setYearTime(String.valueOf(readTime.getYearTime() + dayTime));
            //清除一天的阅读
            readTime.setDayTime("0");
            //更新阅读时间
            readTimeMapper.updateById(readTime);
        }
    }
}
